package com.rt.cms.controller;

import com.rt.cms.common.Constants;
import com.rt.cms.common.base.Page;
import com.rt.cms.common.base.PageResultSet;
import com.rt.cms.common.base.Result;
import com.rt.cms.data.dto.company.ArchivesDto;
import com.rt.cms.data.entity.CompanyInfoBean;
import com.rt.cms.data.entity.TbApprovalRecord;
import com.rt.cms.data.entity.TbCompanyInfo;
import com.rt.cms.data.entity.system.User;
import com.rt.cms.service.ApprovalService;
import com.rt.cms.service.ArchivesService;
import com.rt.cms.service.CompanyInfoService;
import com.rt.cms.service.system.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Create with IDEA
 *
 * @author Wangzs
 * @Description
 * @Date 2018/6/6
 * @Time 15:59
 */
@Controller
@RequestMapping("dormant")
public class DormancyController {

    @Autowired
    private ArchivesService archivesService;
    @Autowired
    private UserService userService;
    @Autowired
    private CompanyInfoService companyInfoService;
    @Autowired
    private ApprovalService approvalService;

    @RequestMapping(method = RequestMethod.GET)
    public String toPage(Model model) {
        return "archives/dormanyCheckList";
    }

    /**
     * 风控审核展示页面
     *
     * @param page
     */
    @ResponseBody
    @RequestMapping("/queryByStatusRiskCheck")
    @RequiresPermissions("dormant:view")
    public PageResultSet<ArchivesDto> queryByStatusRiskCheck(Page page, HttpServletRequest request) {
        String dormant = String.valueOf(Constants.Dormancy);
        User user = (User) request.getAttribute(Constants.CURRENT_USER);
        int currentId = user.getId().intValue();
        List<User> userlist = userService.selectAllUserBypId(currentId);
        String ids = String.valueOf(currentId);
        /*for (int i = 0; i < userlist.size(); i++) {
            ids = ids + "," + userlist.get(i).getId();
        }*/
        return archivesService.findByStatus(0, page, ids, "", 0, dormant,"0");
    }

    /**
     * 分控审核操作
     *
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/checkStatus", method = RequestMethod.POST)
    @RequiresPermissions("dormant:view")
    public Result queryByStatusDepartCheck(TbCompanyInfo tbCompanyInfo, HttpServletRequest request) {

        int operateType = tbCompanyInfo.getOperateType();
        String remark = tbCompanyInfo.getRemark();
        //记录操作记录
        User user = (User) request.getAttribute(Constants.CURRENT_USER);
        Date now = new Date();
        tbCompanyInfo.setCheckOperateId(user.getId().intValue());

        TbApprovalRecord tbApprovalRecord = new TbApprovalRecord();
        tbApprovalRecord.setOperateId(user.getId().intValue());
        tbApprovalRecord.setOperateTime(now);
        tbApprovalRecord.setDemo(remark);
        if (Constants.OutDormancy == Integer.valueOf(operateType)) {
            //申请取消休眠
            tbCompanyInfo.setActiveTime(new Date());
            tbCompanyInfo.setDormant(Constants.unDormancy);
            tbCompanyInfo.setStatus(Constants.Due_Diligence);
            tbApprovalRecord.setOperateType(Constants.OutDormancy);
            tbApprovalRecord.setStatus(Constants.unDormancy);

            //tbApprovalRecord.setIsRead(0);
        }

        //修改企业信息
        if (companyInfoService.editCompanyInfo(tbCompanyInfo, tbApprovalRecord) > 0) {
            //map.put("message", "修改企业信息成功");
        } else {
            //map.put("message", "修改企业信息失败");
        }


        System.out.print(tbCompanyInfo.getId());
        return Result.Success();
    }

    /**
     * 风控审核回显
     *
     * @param
     * @return
     */
    @RequestMapping("/detail")
    // @RequiresPermissions("user:view")
    public String detail(int id, Model model) {

        CompanyInfoBean companyInfoBean = companyInfoService.getCompanyInfoBeanById(id);

        CompanyInfoBean guaranteeCompanyInfoBean = new CompanyInfoBean();
        List<CompanyInfoBean> companyInfoBeans = companyInfoService.getGuaranteeCompanyInfoBean(id);
        if (companyInfoBeans.size() > 0) {
            guaranteeCompanyInfoBean = companyInfoBeans.get(0);
        }
        model.addAttribute("archives", companyInfoBean);
        model.addAttribute("archivesGuarantee", guaranteeCompanyInfoBean);
        return "archives/riskCheckDetail";
    }

    /**
     * 休眠原因
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/historyLoad")
    public String historyLoad(int id, Model model) {
        model.addAttribute("id", id);
        return "archives/dormanyHistoryRecord";
    }

    @RequestMapping("/history")
    @ResponseBody
    public PageResultSet<TbApprovalRecord> history(Page page,String id) {
        return approvalService.queryDormanyRecord(Integer.parseInt(id),page);
    }

    /**
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/dataUpload")
    public String dataUpload(int id, Model model) {

        CompanyInfoBean companyInfoBean = companyInfoService.getCompanyInfoBeanById(id);

        List<CompanyInfoBean> companyInfoBeans = companyInfoService.getGuaranteeCompanyInfoBean(id);

        model.addAttribute("archives", companyInfoBean);

        model.addAttribute("archivesGuarantees", companyInfoBeans);
        return "archives/dormanyDetail";
    }
}
